package cn.com.wangluotongxin.member.controller;

import cn.com.wangluotongxin.common.eureka.CommonClient;
import cn.com.wangluotongxin.common.eureka.form.AuthPhoneCodeForm;
import cn.com.wangluotongxin.common.eureka.form.SendPhoneCodeForm;
import cn.com.wangluotongxin.common.pojo.LoginUser;
import cn.com.wangluotongxin.common.web.AjaxResult;
import cn.com.wangluotongxin.member.form.LoginForm;
import cn.com.wangluotongxin.member.service.IMemberInfoService;
import cn.com.wangluotongxin.member.vo.LoginTokenVo;
import cn.com.wangluotongxin.member.vo.MemberInfoVo;
import cn.com.wangluotongxin.web.utils.AuthUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

@Api(tags = "商户端APP-登录 相关接口")
@RestController
@RequestMapping("/")
public class IndexController {

    @Autowired
    private IMemberInfoService memberInfoService;

    @Autowired
    private CommonClient commonClient;

    @ApiOperation("账号登录")
    @PostMapping("/login/pwd")
    public AjaxResult<LoginTokenVo> loginPwd(@RequestBody LoginForm form){
        Assert.isTrue(StrUtil.isNotBlank(form.getUsername()), "缺少账号");
        Assert.isTrue(StrUtil.isNotBlank(form.getPassword()), "缺少密码");

        String token = memberInfoService.getToken(form.getUsername(), form.getPassword());
        return AjaxResult.success(new LoginTokenVo(token));
    }

    @ApiOperation("发送验证码")
    @PostMapping("/phone/code")
    public AjaxResult phoneCode(@RequestBody LoginForm form){
        Assert.isTrue(StrUtil.isNotBlank(form.getPhone()), "缺少手机号");

        return commonClient.sendPhoneCode(new SendPhoneCodeForm().setPhone(form.getPhone()));
    }

    @ApiOperation("效验验证码")
    @PostMapping("/auth/code")
    public AjaxResult authCode(@RequestBody LoginForm form) {
        Assert.isTrue(StrUtil.isNotBlank(form.getPhone()), "缺少手机号");
        Assert.isTrue(StrUtil.isNotBlank(form.getCode()), "缺少验证码");

        return commonClient.authPhoneCode(new AuthPhoneCodeForm().setPhone(form.getPhone()).setCode(form.getCode()));
    }

    @ApiOperation("手机号登录")
    @PostMapping("/login/phone")
    public AjaxResult<LoginTokenVo> loginPhone(@RequestBody LoginForm form){
        Assert.isTrue(StrUtil.isNotBlank(form.getPhone()), "缺少手机号");
        Assert.isTrue(StrUtil.isNotBlank(form.getCode()), "缺少验证码");
        AjaxResult authResult = commonClient.authPhoneCode(new AuthPhoneCodeForm().setPhone(form.getPhone()).setCode(form.getCode()));
        if(!authResult.isSuccess()) {
            return authResult;
        }
        String token = memberInfoService.getToken(form.getPhone());
        return AjaxResult.success(new LoginTokenVo(token));
    }

    @ApiOperation("用户信息")
    @GetMapping("/info")
    public AjaxResult<MemberInfoVo> info(){
        LoginUser loginUser = AuthUtil.getLoginUser();
        return AjaxResult.success(memberInfoService.getInfo(loginUser.getId()));
    }

    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public AjaxResult logout(){
        memberInfoService.logout(AuthUtil.getLoginUser().getId());
        return AjaxResult.success();
    }
}
